home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Examples / Text / Sources / TextSelc.cpp < prev    next >
Encoding:
Text File  |  1994-04-21  |  15.3 KB  |  534 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                            TextSelc.cpp
  4. //    Release Version:    $ 1.0d1 $
  5. //
  6. //    Author:                        Anthone Burbidge
  7. //    Creation Date:        3/28/94
  8. //
  9. //    Copyright:    © 1993, 1994 by Apple Computer, Inc., all rights reserved.
  10. //
  11. //========================================================================================
  12.  
  13. #ifndef _TEXTPART_
  14. #include "TextPart.h"
  15. #endif
  16.  
  17. #ifndef _TEXTSELC_
  18. #include "TextSelc.h"
  19. #endif
  20.  
  21. #ifndef _VALUESTREAM_
  22. #include "ValueStream.h"
  23. #endif
  24.  
  25. #ifndef _TEXTUTIL_
  26. #include "TextUtil.h"
  27. #endif
  28.  
  29. #ifndef _TEXTFACET_
  30. #include "TextFacet.h"
  31. #endif
  32.  
  33. #ifndef _TEXTPROXY_
  34. #include "TextProxy.h"
  35. #endif
  36.  
  37. #ifndef _EMBEDRUN_
  38. #include "EmbedRun.h"
  39. #endif
  40.  
  41. #ifndef _ODTEXT_
  42. #include "ODText.h"
  43. #endif
  44.  
  45. #ifndef FWRECT_H
  46. #include "FWRect.h"
  47. #endif
  48.  
  49. // ----- Framework Includes -----
  50.  
  51. #ifndef FWMEMMGR_H
  52. #include <FWMemMgr.h>
  53. #endif
  54.  
  55. #ifndef FWFRAME_H
  56. #include "FWFrame.h"
  57. #endif
  58.  
  59. // ----- Textension Includes -----
  60.  
  61. #ifndef _TextensionCommon_
  62. #include "TextensionCommon.h"
  63. #endif
  64.  
  65. #ifndef _Textension_
  66. #include "Textension.h"
  67. #endif
  68.  
  69. // ----- OpenDoc Includes -----
  70.  
  71. #ifndef _STDPROPS_
  72. #include <StdProps.h>
  73. #endif
  74.  
  75. #ifndef _TRNSFORM_
  76. #include <TrnsForm.h>
  77. #endif
  78.  
  79. #ifndef _STDTYPES_
  80. #include <StdTypes.h>
  81. #endif
  82.  
  83. #ifndef _SHAPE_
  84. #include <Shape.h>
  85. #endif
  86.  
  87. #ifndef _TRANSLAM_
  88. #include <TranslaM.h>
  89. #endif
  90.  
  91. #ifndef _XMPSESSM_
  92. #include <XMPSessM.h>
  93. #endif
  94.  
  95. // ----- Macintosh Includes -----
  96.  
  97. #ifndef __FIXMATH__
  98. #include <FixMath.h>
  99. #endif
  100.  
  101. #ifndef mathRoutinesIncludes
  102. #include <Math Routines.h>
  103. #endif
  104.  
  105. #pragma segment TextPartSegment
  106.  
  107. //========================================================================================
  108. // CLASS CTextSelection
  109. //========================================================================================
  110.  
  111. //----------------------------------------------------------------------------------------
  112. // CTextSelection::CTextSelection
  113. //----------------------------------------------------------------------------------------
  114.  
  115. CTextSelection::CTextSelection():
  116.     fTextPart(NULL),
  117.     fIsCloning(FALSE),
  118.     fCommandFrame(NULL)
  119. {
  120. }
  121.  
  122. //----------------------------------------------------------------------------------------
  123. // CTextSelection::InitTextSelection
  124. //----------------------------------------------------------------------------------------
  125.  
  126. void CTextSelection::InitTextSelection(CTextPart* textPart)
  127. {
  128.     InitSelection(textPart, TRUE, TRUE);
  129.     fTextPart = textPart;
  130. }
  131.  
  132. //----------------------------------------------------------------------------------------
  133. // CTextSelection::~CTextSelection
  134. //----------------------------------------------------------------------------------------
  135.  
  136. CTextSelection::~CTextSelection()
  137. {
  138. }
  139.  
  140. //----------------------------------------------------------------------------------------
  141. // CTextSelection::CloseSelection
  142. //----------------------------------------------------------------------------------------
  143.  
  144. void CTextSelection::CloseSelection()
  145. {
  146.     CTextFacet* facet = fTextPart->GetAnyFacet(fTextPart->GetMainPresentation());
  147.     
  148.     CTextDrawInitiator di((CTextFacet *) facet);
  149.     TOffsetRange selRange((long) 0, (long) 0);
  150.     fTextPart->GetEditText()->SetSelectionRange(selRange);
  151. }
  152.  
  153. //----------------------------------------------------------------------------------------
  154. // CTextSelection::SelectAll
  155. //----------------------------------------------------------------------------------------
  156.  
  157. void CTextSelection::SelectAll()
  158. {
  159.     TOffsetRange selRange(0, fTextPart->GetEditText()->CountChars());
  160.     fTextPart->GetEditText()->SetSelectionRange(selRange);
  161. }
  162.  
  163. //----------------------------------------------------------------------------------------
  164. // CTextSelection::IsEmpty
  165. //----------------------------------------------------------------------------------------
  166.  
  167. FW_Boolean CTextSelection::IsEmpty() const
  168. {
  169.     TOffsetRange range;
  170.     fTextPart->GetEditText()->GetSelectionRange(&range);
  171.     
  172.     return range.IsEmpty();
  173. }
  174.  
  175. //----------------------------------------------------------------------------------------
  176. // CTextSelection::DoClear
  177. //----------------------------------------------------------------------------------------
  178.  
  179. FW_Boolean CTextSelection::DoClear()
  180. {
  181.     CTextFacet* facet = fTextPart->GetAnyFacet(fTextPart->GetMainPresentation());
  182.     CTextDrawInitiator di(facet);
  183.     
  184.     TOffsetRange range;
  185.     fTextPart->GetEditText()->GetSelectionRange(&range);
  186.  
  187.     TReplaceParams replaceParams;
  188.     fTextPart->GetEditText()->ReplaceRange(range.Start(), range.End(),
  189.                                            &replaceParams, false);
  190.                                            
  191.     fTextPart->InvalidateAllFrames(fTextPart->GetMainPresentation(), facet, NULL);
  192.     
  193.     return TRUE;
  194. }
  195.  
  196. //----------------------------------------------------------------------------------------
  197. // CTextSelection::IsSelectionOnlyOneProxyRun
  198. //----------------------------------------------------------------------------------------
  199.  
  200. FW_CProxyRun* CTextSelection::IsSelectionOnlyOneProxyRun() const
  201. {
  202.     FW_CProxyRun* proxyRun = NULL;
  203.     CRunsRanges* runsRanges = fTextPart->GetEditText()->GetRunsRanges();
  204.     
  205.     TOffsetRange range;
  206.     fTextPart->GetEditText()->GetSelectionRange(&range);
  207.  
  208.     if (range.Len() == 1)
  209.     {
  210.         long runLength;
  211.         CRunObject* run;
  212.          
  213.         runsRanges->GetNextRun(range.Start(), &run, &runLength);
  214.         if (run->GetClassId() == kEmbeddedPartClassId)
  215.             proxyRun = ((CEmbeddedRun *) run)->GetProxyRun();
  216.     }
  217.     
  218.     return proxyRun;
  219. }
  220.  
  221. //----------------------------------------------------------------------------------------
  222. // CTextSelection::EmbedSelection
  223. //----------------------------------------------------------------------------------------
  224.  
  225. FW_Boolean CTextSelection::EmbedSelection(XMPPart* xmpPart, XMPShape* frameShape)
  226. {
  227.     FW_CRect shapeRect;
  228.     if (frameShape)
  229.     {
  230.         frameShape->GetBoundingBox(&shapeRect);
  231.         shapeRect.Offset(ff(-shapeRect.left), ff(-shapeRect.top));
  232.     }
  233.     else
  234.     {
  235.         shapeRect.Set(ff(0), ff(0), ff(80), ff(80));
  236.     }
  237.     
  238.     fTextPart->HandleEmbedPart(xmpPart, shapeRect);
  239.     
  240.     return TRUE;
  241. }
  242.  
  243. //----------------------------------------------------------------------------------------
  244. // CTextSelection::ExternalizeSelection_ContentPropertyValueType
  245. //----------------------------------------------------------------------------------------
  246.  
  247. void CTextSelection::ExternalizeSelection_ContentPropertyValueType
  248.     (XMPStorageUnit* storageUnit, FW_CFrame* commandFrame, XMPCloneKind cloneKind)
  249. {
  250. FW_UNUSED(cloneKind);
  251.  
  252.     storageUnit->AddValue(this->GetPart()->GetContentPropertyValueType());
  253.  
  254.     CValueStream *stream = new CValueStream;
  255.     stream->InitValueStream(storageUnit);
  256.     
  257.     CTextensionIOSuite *ioSuite = new CTextensionIOSuite;
  258.     ioSuite->ITextensionIOSuite(false, stream);
  259.     
  260.     CTextPart::gCurrentPart = fTextPart;
  261.     fIsCloning = TRUE;
  262.     fCommandFrame = commandFrame;
  263.     
  264.     TOffsetRange range2Save;
  265.     fTextPart->GetEditText()->GetSelectionRange(&range2Save);
  266.     OSErr err = fTextPart->GetEditText()->GetRangeIOSuite(&range2Save, ioSuite);
  267.  
  268.     fCommandFrame = NULL;
  269.     fIsCloning = FALSE;
  270.     CTextPart::gCurrentPart = NULL;
  271.  
  272.     ioSuite->Free();
  273.     delete ioSuite;
  274.     
  275.     stream->Free();
  276.     delete stream;
  277. }
  278.  
  279. //----------------------------------------------------------------------------------------
  280. // CTextSelection::ExternalizeSelection_TEXT
  281. //----------------------------------------------------------------------------------------
  282.  
  283. void CTextSelection::ExternalizeSelection_TEXT(XMPStorageUnit* storageUnit,
  284.                                                FW_CFrame* commandFrame,
  285.                                                XMPCloneKind cloneKind)
  286. {
  287. FW_UNUSED(cloneKind);
  288. FW_UNUSED(commandFrame);
  289.  
  290.     XMPTranslation *translate = GetPart()->GetSession()->GetTranslation();
  291.     XMPType kAppleTEXT = translate->GetISOTypeFromPlatformType('TEXT', kXMPPlatformDataType);
  292.     
  293.     storageUnit->AddValue(kAppleTEXT);
  294.  
  295.     CValueStream *stream = new CValueStream;
  296.     stream->InitValueStream(storageUnit);
  297.     
  298.     CTextensionIOSuite *ioSuite = new CTextensionIOSuite;
  299.     ioSuite->ITextensionIOSuite(false, stream);
  300.     
  301.     TOffsetRange range2Save;
  302.     fTextPart->GetEditText()->GetSelectionRange(&range2Save);
  303.     OSErr err = fTextPart->GetEditText()->GetRangeIOSuite(&range2Save, ioSuite, kIOText);
  304.  
  305.     ioSuite->Free();
  306.     delete ioSuite;
  307.     
  308.     stream->Free();
  309.     delete stream;
  310. }
  311.  
  312. //----------------------------------------------------------------------------------------
  313. // CTextSelection::ExternalizeSelection
  314. //----------------------------------------------------------------------------------------
  315.  
  316. void CTextSelection::ExternalizeSelection(XMPStorageUnit* storageUnit,
  317.                                           FW_CFrame* commandFrame, XMPCloneKind cloneKind)
  318. {
  319.     storageUnit->AddProperty(kXMPPropContents);
  320.     
  321.     this->ExternalizeSelection_ContentPropertyValueType(storageUnit, commandFrame, cloneKind);
  322.     this->ExternalizeSelection_TEXT(storageUnit, commandFrame, cloneKind);
  323. }
  324.  
  325. //----------------------------------------------------------------------------------------
  326. // CTextSelection::InternalizeSelection_ContentPropertyValueType
  327. //----------------------------------------------------------------------------------------
  328.  
  329. FW_Boolean CTextSelection::InternalizeSelection_ContentPropertyValueType
  330.             (XMPStorageUnit* storageUnit, XMPCloneKind cloneKind)
  331. {
  332. FW_UNUSED(cloneKind);
  333.  
  334.     // ----- Focus on content property -----
  335.     storageUnit->Focus(kXMPPropContents, kXMPPosUndefined, 
  336.                           this->GetPart()->GetContentPropertyValueType(),
  337.                        (XMPValueIndex)0, kXMPPosUndefined);
  338.  
  339.     CTextFacet* facet = fTextPart->GetAnyFacet(fTextPart->GetMainPresentation());
  340.     CTextDrawInitiator di(facet);
  341.  
  342.     CValueStream *stream = new CValueStream;
  343.     stream->InitValueStream(storageUnit);
  344.     
  345.     CTextensionIOSuite *ioSuite = new CTextensionIOSuite;
  346.     ioSuite->ITextensionIOSuite(false, stream);
  347.     
  348.     CTextPart::gCurrentPart = fTextPart;
  349.     fIsCloning = TRUE;
  350.     
  351.     TOffsetRange range2Replace;
  352.     fTextPart->GetEditText()->GetSelectionRange(&range2Replace);
  353.     TReplaceParams replaceParams(ioSuite);
  354.     OSErr err = fTextPart->GetEditText()->ReplaceRange(range2Replace.Start(),
  355.                                                        range2Replace.End(),
  356.                                                        &replaceParams, false);
  357.  
  358.     fTextPart->InvalidateAllFrames(fTextPart->GetMainPresentation(), facet, NULL);
  359.     
  360.     fIsCloning = FALSE;
  361.     CTextPart::gCurrentPart = NULL;
  362.         
  363.     ioSuite->Free();
  364.     delete ioSuite;
  365.     
  366.     stream->Free();
  367.     delete stream;
  368.     
  369.     return TRUE;
  370. }
  371.  
  372. //----------------------------------------------------------------------------------------
  373. // CTextSelection::InternalizeSelection_TEXT
  374. //----------------------------------------------------------------------------------------
  375.  
  376. FW_Boolean CTextSelection::InternalizeSelection_TEXT
  377.             (XMPStorageUnit* storageUnit, XMPCloneKind cloneKind)
  378. {
  379. FW_UNUSED(cloneKind);
  380.  
  381.     XMPTranslation *translate = GetPart()->GetSession()->GetTranslation();
  382.     XMPType kAppleTEXT = translate->GetISOTypeFromPlatformType('TEXT', kXMPPlatformDataType);
  383.  
  384.     storageUnit->Focus(kXMPPropContents, kXMPPosUndefined, kAppleTEXT,
  385.                        (XMPValueIndex)0, kXMPPosUndefined);
  386.                    
  387.     CTextFacet* facet = fTextPart->GetAnyFacet(fTextPart->GetMainPresentation());
  388.     CTextDrawInitiator di(facet);
  389.  
  390.     unsigned long textSize = storageUnit->GetSize();
  391.     FW_PlatformHandle textHandle = FW_CMemoryManager::AllocateSystemHandle(textSize);
  392.     
  393.     FW_CMemoryManager::LockSystemHandle(textHandle);
  394.     storageUnit->GetValue(textSize, (XMPValue)*textHandle);
  395.     
  396.     TOffsetRange range2Replace;
  397.     fTextPart->GetEditText()->GetSelectionRange(&range2Replace);
  398.     TReplaceParams replaceParams(TTextDescriptor((unsigned char *) *textHandle, textSize));
  399.     OSErr err = fTextPart->GetEditText()->ReplaceRange(range2Replace.Start(),
  400.                                                        range2Replace.End(),
  401.                                                        &replaceParams, false);
  402.  
  403.     fTextPart->InvalidateAllFrames(fTextPart->GetMainPresentation(), facet, NULL);
  404.  
  405.     FW_CMemoryManager::UnlockSystemHandle(textHandle);
  406.     FW_CMemoryManager::FreeSystemHandle(textHandle);
  407.  
  408.     return TRUE;
  409. }
  410.  
  411. //----------------------------------------------------------------------------------------
  412. // CTextSelection::InternalizeSelection
  413. //----------------------------------------------------------------------------------------
  414.  
  415. FW_Boolean CTextSelection::InternalizeSelection(XMPStorageUnit* storageUnit,
  416.                                                 XMPCloneKind cloneKind)
  417. {
  418. FW_UNUSED(cloneKind);
  419.  
  420.     FW_Boolean success = FALSE;
  421.  
  422.     XMPTranslation *translate = GetPart()->GetSession()->GetTranslation();
  423.     XMPType kAppleTEXT = translate->GetISOTypeFromPlatformType('TEXT', kXMPPlatformDataType);
  424.  
  425.     if (storageUnit->Exists(kXMPPropContents, this->GetPart()->GetContentPropertyValueType(), 0))
  426.         success = this->InternalizeSelection_ContentPropertyValueType(storageUnit, cloneKind);
  427.     else if (storageUnit->Exists(kXMPPropContents, kAppleTEXT, 0))
  428.         success = this->InternalizeSelection_TEXT(storageUnit, cloneKind);
  429.     
  430.     return success;
  431. }
  432.  
  433. //----------------------------------------------------------------------------------------
  434. // CTextSelection::CalcDragRgn
  435. //----------------------------------------------------------------------------------------
  436.  
  437. XMPRgnHandle CTextSelection::CalcDragRgn(FW_CFacet* facet)
  438. {
  439. FW_UNUSED(facet);
  440.  
  441.     // Just temporary until I get a Textension that provides me with an easy method to
  442.     // get the region of the current selection.
  443.  
  444.     TOffsetRange selection;
  445.     fTextPart->GetEditText()->GetSelectionRange(&selection);
  446.     
  447.     long top, trailEdge;
  448.     short hite;
  449.     FW_SPlatformRect rt;
  450.     
  451.     fTextPart->GetEditText()->Char2Point(selection.Start(), &top, &trailEdge, &hite);
  452.     
  453.     rt.top = (short) top;
  454.     rt.left = (short) trailEdge;
  455.     
  456.     fTextPart->GetEditText()->Char2Point(selection.End(), &top, &trailEdge, &hite);
  457.     
  458.     rt.bottom = (short) top + hite;
  459.     rt.right = (short) trailEdge;
  460.     
  461.     XMPRgnHandle dragRgn = ::NewRgn();
  462.     FW_PlatformRegion tempRgn = ::NewRgn();
  463.  
  464.     if (selection.Len() > 0)
  465.     {
  466.         ::RectRgn(dragRgn, &rt);
  467.         ::CopyRgn(dragRgn, tempRgn);
  468.         ::InsetRgn(tempRgn, 1, 1);
  469.         ::DiffRgn(dragRgn, tempRgn, dragRgn);
  470.     }
  471.     
  472.     ::DisposeRgn(tempRgn);
  473.     
  474.     return dragRgn;
  475. }
  476.  
  477. //----------------------------------------------------------------------------------------
  478. // CTextSelection::DoDrop
  479. //----------------------------------------------------------------------------------------
  480.  
  481. FW_Boolean CTextSelection::DoDrop(XMPStorageUnit* dropSU, FW_CFacet* facet,
  482.                                   const FW_CPoint& originPoint, const FW_CPoint& dropPoint)
  483. {
  484.     // Set the Textension selection
  485.     
  486.     TOffsetRange range;
  487.     Boolean outside;
  488.     
  489.     FW_CPoint where = facet->GetWindowContentTransform()->InvertPoint(dropPoint);
  490.     FW_SPlatformPoint pt;
  491.     where.AsPlatformPoint(pt);
  492.     
  493.     CTextDrawInitiator di((CTextFacet *) facet);
  494.     
  495.     fTextPart->GetEditText()->Point2Char(pt, &range, &outside);
  496.     fTextPart->GetEditText()->SetSelectionRange(range);
  497.     
  498.     return FW_CSelection::DoDrop(dropSU, facet, originPoint, dropPoint);
  499. }
  500.  
  501. //----------------------------------------------------------------------------------------
  502. // CTextSelection::DoDroppedInSameFrame
  503. //----------------------------------------------------------------------------------------
  504.  
  505. FW_Boolean CTextSelection::DoDroppedInSameFrame(XMPStorageUnit* dropSU, FW_CFacet* facet,
  506.                                                 const FW_CPoint& originPoint, const FW_CPoint& dropPoint)
  507. {
  508.     return this->DoDrop(dropSU, facet, originPoint, dropPoint);
  509. }
  510.  
  511. //----------------------------------------------------------------------------------------
  512. // CTextSelection::IsPointInSelection
  513. //----------------------------------------------------------------------------------------
  514.  
  515. FW_Boolean CTextSelection::IsPointInSelection(const FW_CPoint& xmpPt)
  516. {
  517.     FW_SPlatformPoint pt;
  518.     xmpPt.AsPlatformPoint(pt);
  519.     
  520.     TOffsetRange selectionRange;
  521.     fTextPart->GetEditText()->GetSelectionRange(&selectionRange);
  522.     
  523.     Boolean outside;
  524.     TOffsetRange charRange;
  525.     fTextPart->GetEditText()->Point2Char(pt, &charRange, &outside);
  526.     
  527.     if (charRange.Start() >= selectionRange.Start()
  528.         && charRange.End() <= selectionRange.End())
  529.         return TRUE;
  530.     else
  531.         return FALSE;
  532. }
  533.  
  534.